package com.itbaizhan.service.impl;

import com.itbaizhan.dao.DetailDao;
import com.itbaizhan.pojo.Item;
import com.itbaizhan.service.DetailService;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author spf
 * @date 2024/3/4
 * @time 15:26
 */
@Service
public class DetailServiceImpl implements DetailService {

    @Autowired
    private DetailDao detailDao;
    @Value("${livegoods.banner.nginx.prefix}")
    private String nginxPrefix;

    @Override
    @Cacheable(cacheNames = "com:livegoods:details", key = "'getDetail('+#p0+')'")
    @RateLimiter(name = "backendA")
    @CircuitBreaker(name = "backendA")
    public Item getDetails(String id) {
        Item item = detailDao.findById(id);
        ArrayList<String> imageUrl = new ArrayList();
        for (String img : item.getImgs()) {
            imageUrl.add(nginxPrefix + img);
        }
        item.setImgs(imageUrl);
        return item;
    }
}
